package com.bonc.common.sso;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class AsiaEncode {
	private static String DEFAULT_CHARSET = "utf-8";
	private static int MD5_BEGIN = 8;
	private static int MD5_END = 24;
	private static int MD5_LEN = 16;
	private static int MD5_INDEX = 0;
	private static int DATA_MINLEN = 16;
	private static int CONTENT_INDEX = 16;

	public static String encryptData(String message, String charSet) {
		String outputStr = null;
		try {
			EncryptAES encryptAES = EncryptAES.getInstance(EncryptAES.DEFAULT_KEY,charSet);
			// 对原文加密
			outputStr = HexTransfer.byteArr2HexStr(encryptAES.Encrytor(message,
					charSet));
			// 对加密后的密文计算md5码
			String md5Str = HexTransfer.byteArr2HexStr(EncryptMD5
					.encrypt(outputStr));
			// 组装新报文为“32位定长md5+变长密文”
			outputStr = md5Str.substring(MD5_BEGIN, MD5_END) + outputStr;
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}
		return outputStr;
	}

	public static String encryptData(String message) {
		return encryptData(message, DEFAULT_CHARSET);
	}

	public static String dencryptData(String message, String charSet) {
		if (charSet == null || "".equals(charSet)) {
			charSet = DEFAULT_CHARSET;
		}
		String data = null;// 客户端解密后的明文
		if (message != null && message.length() > DATA_MINLEN) {
			String md5Head = message.substring(MD5_INDEX, MD5_LEN);
			// 一次性全部取出密文然后解密
			String content = message.substring(CONTENT_INDEX, message.length());
			String md5Str = null;// 客户端计算的md5
			try {
				// 重新计算md5码
				md5Str = HexTransfer
						.byteArr2HexStr(EncryptMD5.encrypt(content)).substring(
								MD5_BEGIN, MD5_END);
			} catch (NoSuchAlgorithmException e1) {
				e1.printStackTrace();
			}
			if (md5Head.equals(md5Str)) {
				try {
					EncryptAES encryptAES = EncryptAES.getInstance(EncryptAES.DEFAULT_KEY,charSet);
					// 解密
					data = new String(encryptAES.Decryptor(HexTransfer
							.hexStr2ByteArr(content)), charSet);
				} catch (InvalidKeyException e) {
					e.printStackTrace();
				} catch (IllegalBlockSizeException e) {
					e.printStackTrace();
				} catch (BadPaddingException e) {
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				} catch (NoSuchAlgorithmException e) {
					e.printStackTrace();
				} catch (NoSuchPaddingException e) {
					e.printStackTrace();
				}
			}

		}
		return data;

	}

	public static String dencryptData(String message) {
		return dencryptData(message, DEFAULT_CHARSET);
	}

	public static String encrypt(String message, String charSet) {
		String outputStr = null;
		try {
			// 对原文加密
			outputStr = EncryptAES.encrypt(message, EncryptAES.DEFAULT_KEY, charSet);
			// 对加密后的密文计算md5码
			String md5Str = HexTransfer.byteArr2HexStr(EncryptMD5
					.encrypt(outputStr));
			// 组装新报文为“32位定长md5+变长密文”
			outputStr = md5Str.substring(MD5_BEGIN, MD5_END) + outputStr;
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}
		return outputStr;
	}
	
	public static String encrypt(String message) {
		return encrypt(message, DEFAULT_CHARSET);
	}
	
	public static String dencrypt(String message, String charSet) {
		if (charSet == null || "".equals(charSet)) {
			charSet = DEFAULT_CHARSET;
		}
		String data = null;// 客户端解密后的明文
		if (message != null && message.length() > DATA_MINLEN) {
			String md5Head = message.substring(MD5_INDEX, MD5_LEN);
			// 一次性全部取出密文然后解密
			String content = message.substring(CONTENT_INDEX, message.length());
			String md5Str = null;// 客户端计算的md5
			try {
				// 重新计算md5码
				md5Str = HexTransfer
						.byteArr2HexStr(EncryptMD5.encrypt(content)).substring(
								MD5_BEGIN, MD5_END);
			} catch (NoSuchAlgorithmException e1) {
				e1.printStackTrace();
			}
			if (md5Head.equals(md5Str)) {
				try {
					data = EncryptAES.decrypt(content, EncryptAES.DEFAULT_KEY,charSet);
				} catch (InvalidKeyException e) {
					e.printStackTrace();
				} catch (IllegalBlockSizeException e) {
					e.printStackTrace();
				} catch (BadPaddingException e) {
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				} catch (NoSuchAlgorithmException e) {
					e.printStackTrace();
				} catch (NoSuchPaddingException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
					
				}
			}
			
		}
		return data;
		
	}
	public static String dencrypt(String message) {
		return dencrypt(message, DEFAULT_CHARSET);
	}

	public static void main(String[] args) {
//		String a = "亚信科技2017";
//		String b = AsiaEncode.encrypt(a,"utf-8");
//		String c = AsiaEncode.dencrypt("cae339e99de7d9b9cd4ab3a985775b29336cccdecaa7649f84b7497bfc32999786d28ca5cd00e436","utf-8");
//		System.err.println("原文：" + a);
//		System.err.println("加密：" + b);
//		System.err.println("解密：" + c);
		String a = "亚信科技2017";
		String b = AsiaEncode.encrypt(a,"utf-8");
		String c = AsiaEncode.dencrypt(b,"utf-8");
		System.err.println("原文：" + a);
		System.err.println("加密：" + b);
		System.err.println("解密：" + c);
	}
}
